﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Contract = ChatForum.Business.Contract;
using Entity = ChatForum.Business.Entity;

namespace ChatForum.Client
{
    public partial class Register : BasePage
    {
        internal static Entity.User CurrentUser
        {
            get
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                    return ServiceHelper.ServiceHelper.AccountService.GetUser(HttpContext.Current.User.Identity.Name);
                else
                    return null;
            }
        }

        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (User.Identity.IsAuthenticated)
            {
                this.MasterPageFile = "~/MemberLayout.master";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (User.Identity.IsAuthenticated)//If there is already a Logged In user, then load details
                {
                    PageHelper.HideControl(this, "accountFieldset");
                    register.Text = "Save";

                    txtFirstName.Text = CurrentUser.FirstName;
                    txtMiddleName.Text = CurrentUser.MiddleName;
                    txtLastName.Text = CurrentUser.LastName;
                    ddSex.Text = CurrentUser.Sex;
                    txtDOB.Text = CurrentUser.DateOfBirth.ToShortDateString();
                    ddCountry.Text = CurrentUser.Country;
                    txtMobile.Text = CurrentUser.MobileNo;
                    txtEmail.Text = CurrentUser.EmailId;
                    txtAlternateEmail.Text = CurrentUser.AlternateEmailId;
                }
            }
        }

        protected void OnRegister(object sender, EventArgs e)
        {
            DateTime outDateTime;
            ChatForum.Business.Entity.User user = new Business.Entity.User()
            {
                FirstName = txtFirstName.Text,
                MiddleName = txtMiddleName.Text,
                LastName = txtLastName.Text,
                UserId = txtUserId.Text,
                Sex = ddSex.Text,
                DateOfBirth = DateTime.TryParse(txtDOB.Text, out outDateTime) ? outDateTime : DateTime.Now,
                Country = ddCountry.Text,
                MobileNo = txtMobile.Text,
                EmailId = txtEmail.Text,
                AlternateEmailId = txtAlternateEmail.Text,
                Password = txtPassword.Text
            };
            //If it is an Existing, logged in user, then fill some additional details,
            //which are not modified from UI, but need to store in DB
            if (User.Identity.IsAuthenticated)
            {
                user.ID = CurrentUser.ID;
                user.UserId = CurrentUser.UserId;
                user.Password = CurrentUser.Password;
                user.Role = CurrentUser.Role;
                user.Status = CurrentUser.Status;
                user.IsOnline = CurrentUser.IsOnline;
                user.LastLogInTime = CurrentUser.LastLogInTime;

                Handler.ModifyAccount(user);
            }
            else
                Handler.OnRegister(user);
        }
    }
}